<!DOCTYPE html>
<html>
<head>
<title>内容管理</title>
#include("../include/common.html")
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>内容管理</h5>
            </div>
            <form id="listForm" action="data" method="get">
			  #if(categoryId??)<input type="hidden" name="categoryId" value="#(categoryId)">#end
			  <div class="ibox-content">
			      <!-- 搜索 start -->
			      <div class="form-horizontal clear">
			          <div class="form-group">
			              <label class="col-sm-2 control-label">标题：</label>
			              <div class="col-sm-2"><input class="form-control" type="text" name="title" value="#(title)"></div>
			              <label class="col-sm-2 control-label">类型：</label>
                          <div class="col-sm-2">
                            <select class="form-control" name="type">
                            <option value="">--请选择--</option>
                            #for(selfTypeKey : selfTypes.keySet())
                                <option value="#(selfTypeKey)" #if(type==selfTypeKey) selected #end>#(selfTypes.get(selfTypeKey))</option>
                            #end
                            </select>
                          </div>
						  #if(categoryId?? && categoryId==50)
						  <label class="col-sm-2 control-label">审核状态：</label>
						  <div class="col-sm-2">
							  <select class="form-control" name="status">
								  <option value="">--请选择--</option>
								  #for(isCheck : checks.keySet())
								  <option value="#(isCheck)" #if(status==isCheck) selected #end>#(checks.get(isCheck))</option>
								  #end
							  </select>
						  </div>
						  #end
			          </div>
			          <div class="text-center">
			              <input type="submit" class="btn btn-primary btn-w-m" value="查 询">
			              <input type="reset" class="btn btn-primary btn-w-m" value="重 置">
			          </div>
			      </div>
			      <!-- 搜索 end -->
			      <!-- 列表 start -->
			      <div class="form-horizontal clear">
			          <div class="form-group">
			              <div class="col-sm-8">
			                  #if(categoryId??)<a class="btn btn-primary"  href="add?categoryId=#(categoryId)"><i class="fa fa-plus"></i> 添加</a>#end
			                  <a href="javascript:;" class="btn btn-warning disabled" id="deleteButton"><i class="fa fa-trash"></i> 删除</a>
			                  <a href="javascript:;" class="btn btn-primary" id="refreshButton"><i class="fa fa-refresh"></i> 刷新</a>
			                  <a href="javascript:;" class="btn btn-primary" id="sortButton"><i class="fa fa-sort"></i> 排序</a>
							  #if(categoryId?? && categoryId==50)<a href="javascript:;" class="btn btn-warning disabled" id="reviewButton"><i class="fa fa-check"></i> 审核</a>#end
			              </div>
			          </div>
			          <div class="table-responsive">
			              <table class="table table-bordered" id="listTable">
			                  <thead>
			                      <tr>
			                          <th width="40" class="text-center"><input type="checkbox" id="selectAll" /></th>
			                          <th>ID</th>
			                          <th>标题</th>
			                          <th>栏目</th>
			                          <th>创建日期</th>
			                          <th>类型</th>
									  #if(categoryId?? && categoryId==50)<th>审核</th>#end
			                          <th width="80">排序</th>
			                          <th>操作</th>
			                      </tr>
			                  </thead>
			                  <tbody id="listTr">
			                      #for(content : page.list)   
			                      <tr>
			                          <td class="text-center"><input type="checkbox" name="ids" value="#(content.id)" /></td>
			                          <td>#(content.id)</td>
			                          <td>#(content.title)</td>
			                          <td>#(content.category.name)</td>
			                          <td>#(content.createDate)</td>
			                          <td>#(selfTypes.get(content.type))</td>
									  #if(categoryId?? && categoryId==50)<td>#if(content.isCheck?? && content.isCheck==1)已审核#end #if(content.isCheck?? && content.isCheck==0)未审核#end</td>#end
			                          <td><input type="text" class="form-control" name="sorts" value="#(content.sort)" data-oldsort="#(content.sort)" data-id="#(content.id)"/></td>
			                          <td>
			                              <a href="edit?id=#(content.id)&categoryId=#(categoryId)">[编辑]</a>
			                          </td>
			                      </tr>
			                      #end
			                  </tbody>
			              </table>
			          </div>
			          <div class="text-center">
			              #pagination(pageNumber = page.pageNumber,totalPages = page.totalPage,pattern = "")
			                   #include("../include/pagination.html")
			              #end
			          </div>
			      </div>
			      <!-- 列表 end -->
			    </div>
			</form>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
$().ready(function() {

    var $sortButton = $("#sortButton");
    
    //排序
    $sortButton.click(function() {
        var $this = $(this);
        var $sortArry = [];
        $.each($("#listTr tr"),function(index,item){
            var $sorts = $(item).find("input[name='sorts']");
            var $sort = $sorts.val();
            var $oldsort = $sorts.data("oldsort");
            var $id = $sorts.data("id");
            if($sort!=$oldsort){
                var $sortReg = /^[1-9]\d*|0$/;
                if (!($sortReg.test($sort))) { 
                   $sort = "0";
                }
                var $json = {"id":$id,"sort":$sort};
                $sortArry.push($json);
            }
        });
        if($sortArry.length<1){
            swal({title:'', text:'数据未修改!',icon: 'warning'});
            return false;
        }
        
        
        /*判断排序信息*/
		var categoryId=$("input[name='categoryId']").val();
        if(categoryId!=50){
        	$.ajax({
                url: "updateSort",
                type: "POST",
                data: {sortArray: JSON.stringify($sortArry)},
                dataType: "json",
                cache: false,
                success: function(message) {
                    if (message.type == "success") {
                        location.reload();
                    }else{
                        swal({title:'', text:message.msg,icon: 'error'});
                    }
                }
            });
        }else{
        	$.ajax({
                url: "updateSortPushContent",
                type: "POST",
                data: {sortArray: JSON.stringify($sortArry)},
                dataType: "json",
                cache: false,
                success: function(message) {
                    if (message.type == "success") {
                        location.reload();
                    }else{
                        swal({title:'', text:message.msg,icon: 'error'});
                    }
                }
            });
        }
        
        return false;
    });

});

</script>